package com.shop.cereshop.app.controller.report;

import com.shop.cereshop.app.annotation.NoRepeatSubmit;
import com.shop.cereshop.app.annotation.NoRepeatWebLog;
import com.shop.cereshop.app.page.report.ReportRecordVO;
import com.shop.cereshop.app.param.report.ReportPageDTO;
import com.shop.cereshop.app.param.report.MyReportPageDTO;
import com.shop.cereshop.app.service.report.CereReportRecordService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.report.dto.ReportSubmitDTO;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * 举报功能控制器
 *
 * @author
 */
@RestController
@RequestMapping("report")
@Slf4j(topic = "ReportController")
@Api(value = "举报功能", tags = "举报功能")
public class ReportController {

    @Autowired
    private CereReportRecordService cereReportRecordService;

    /**
     * 提交举报
     */
    @PostMapping("submit")
    @NoRepeatSubmit
    @NoRepeatWebLog
    @ApiOperation(value = "提交举报")
    public Result submitReport(@Validated @RequestBody ReportSubmitDTO param, HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return new Result<>(CoReturnFormat.USER_NOT_LOGIN);
        }

        try {
            Long reportId = cereReportRecordService.submitReport(param, user);
            return Result.success(reportId);
        } catch (CoBusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("提交举报失败", e);
            return Result.failed(CoReturnFormat.FAIL);
        }
    }

    /**
     * 分页查询举报记录
     */
    @PostMapping("page")
    @ApiOperation(value = "分页查询举报记录")
    public Result getReportPage(@RequestBody ReportPageDTO param, HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return new Result<>(CoReturnFormat.USER_NOT_LOGIN);
        }

        try {
            Page<ReportRecordVO> page = cereReportRecordService.getReportPage(param, user);
            return Result.success(page);
        } catch (CoBusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("查询举报记录失败", e);
            return Result.failed(CoReturnFormat.FAIL);
        }
    }

    /**
     * 获取举报详情
     */
    @GetMapping("detail")
    @ApiOperation(value = "获取举报详情")
    public Result getReportDetail(@NotNull(message = "举报ID不能为空")
                                  @RequestParam("reportId")
                                  @ApiParam(value = "举报ID", required = true) Long reportId,
                                  HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return new Result<>(CoReturnFormat.USER_NOT_LOGIN);
        }

        try {
            ReportRecordVO detail = cereReportRecordService.getReportDetail(reportId, user);
            return Result.success(detail);
        } catch (CoBusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取举报详情失败", e);
            return Result.failed(CoReturnFormat.FAIL);
        }
    }

    /**
     * 撤销举报
     */
    @DeleteMapping("cancel")
    @NoRepeatSubmit
    @NoRepeatWebLog
    @ApiOperation(value = "撤销举报")
    public Result cancelReport(@NotNull(message = "举报ID不能为空")
                               @RequestParam("reportId")
                               @ApiParam(value = "举报ID", required = true) Long reportId,
                               HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return new Result<>(CoReturnFormat.USER_NOT_LOGIN);
        }

        try {
            Boolean result = cereReportRecordService.cancelReport(reportId, user);
            return Result.success(result);
        } catch (CoBusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("撤销举报失败", e);
            return Result.failed(CoReturnFormat.FAIL);
        }
    }

    /**
     * 检查是否已举报
     */
    @GetMapping("check")
    @ApiOperation(value = "检查是否已举报")
    public Result<Boolean> hasReported(@NotNull(message = "目标类型不能为空")
                                       @RequestParam("targetType")
                                       @ApiParam(value = "目标类型：1-动态 2-视频 3-评论 4-用户", required = true) Integer targetType,
                                       @NotNull(message = "目标ID不能为空")
                                       @RequestParam("targetId")
                                       @ApiParam(value = "目标ID", required = true) Long targetId,
                                       HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return new Result<>(CoReturnFormat.USER_NOT_LOGIN);
        }

        try {
            Boolean hasReported = cereReportRecordService.hasReported(targetType, targetId, user);
            return Result.success(hasReported);
        } catch (Exception e) {
            log.error("检查举报状态失败", e);
            return Result.failed(CoReturnFormat.FAIL);
        }
    }

    /**
     * 获取我的举报列表
     */
    @PostMapping("my")
    @ApiOperation(value = "获取我的举报列表")
    public Result getMyReports(@RequestBody MyReportPageDTO param, HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return new Result<>(CoReturnFormat.USER_NOT_LOGIN);
        }

        try {
            Page<ReportRecordVO> page = cereReportRecordService.getMyReports(param, user);
            return Result.success(page);
        } catch (CoBusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取我的举报列表失败", e);
            return Result.failed(CoReturnFormat.FAIL);
        }
    }

    /**
     * 获取举报统计信息
     */
    @GetMapping("stats")
    @ApiOperation(value = "获取举报统计信息")
    public Result getReportStats(HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return new Result<>(CoReturnFormat.USER_NOT_LOGIN);
        }

        try {
            Object stats = cereReportRecordService.getReportStats(user);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取举报统计信息失败", e);
            return Result.failed(CoReturnFormat.FAIL);
        }
    }
}
